import { Provide } from '@midwayjs/core';
import { Pagination } from '../model';
import { Visitor } from '../entity/visitor.entity';
import { House } from '../entity/house.entity';
import { User } from '../entity/user.entity';
import { VisitorAddDto, VisitorIdRequired } from '../model/visitor.dto';
import dayjs = require("dayjs");
class VisitorUseDTO {
  pointName: string;
  building: string;
  room: string;
  userName: string;
  constructor(visitor: any) {
    if (visitor.house) {
      this.pointName = visitor.house.pointName;
      this.building = visitor.house.building;
      this.room = visitor.house.room;
    }
    if (visitor.user) {
      this.userName = visitor.user.name;
    }
    if (visitor.visitDate) {
      visitor.visitDate = dayjs(visitor.visitDate).format('YYYY-MM-DD HH:mm:ss')
    }
    delete visitor.house;
    delete visitor.repairItem;
    delete visitor.user;
    Object.assign(this, visitor);
  }
}
@Provide()
export class VisitorService {
  async getList(req: Pagination) {
    const { count, rows } = await Visitor.findAndCountAll({
      limit: req.pageSize,
      offset: (req.pageNo - 1) * req.pageSize,
      attributes: { exclude: ['createdAt', 'updatedAt'] },
      include: [
        {
          model: House,
          attributes: ['pointName', 'building', 'room'],
        },
        {
          model: User,
          attributes: ['name'],
        },
      ],
      order:[['id','DESC']]
    });
    const list = rows.map(
      visitor => new VisitorUseDTO(visitor.get({ plain: true }))
    );
    return {
      count,
      rows: list,
    };
  }

  async listByPerson(req: Pagination, userId: number) {
    const { count, rows } = await Visitor.findAndCountAll({
      where: {
        userId: userId,
      },
      limit: req.pageSize,
      offset: (req.pageNo - 1) * req.pageSize,
      attributes: { exclude: ['createdAt', 'updatedAt'] },
      include: [
        {
          model: House,
          attributes: ['pointName', 'building', 'room'],
        },
        {
          model: User,
          attributes: ['name'],
        },
      ],
      order:[['id','DESC']]
    });
    const list = rows.map(
      repair => new VisitorUseDTO(repair.get({ plain: true }))
    );
    return {
      count,
      rows: list,
    };
  }

  async byIdItem(req: VisitorIdRequired) {
    const result = await Visitor.findOne({
      where: {
        id: req.id,
      },
      attributes: { exclude: ['createdAt', 'updatedAt'] },
      include: [
        {
          model: House,
          attributes: ['pointName', 'building', 'room'],
        },
        {
          model: User,
          attributes: ['name'],
        },
      ],
    });
    const visitDate = new Date(result.visitDate).getTime();

    const now = new Date().getTime();
    if (now > visitDate) {
      result.status = '0';
      Visitor.update(
        {
          status: '0',
        },
        {
          where: {
            id: req.id,
          },
        }
      );
    }
    const newResult = new VisitorUseDTO(result.get({ plain: true }));
    return newResult;
  }

  async addItem(req: VisitorAddDto,status:string, userId: number) {
    const result = await Visitor.create({
      ...req,
      status,
      userId: userId,
    })
    return result;
  }
}
